package io.aircraft.modules.wms.controller;

import io.aircraft.modules.wms.dto.HomeShowDTO;
import io.aircraft.modules.wms.service.LocationService;
import io.aircraft.common.annotation.LogOperation;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.LocationDTO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 货位信息
 *
 * @author Cycle cycle@gmail.com
 * @since 1.0.0 2022-10-07
 */
@RestController
@RequestMapping("wms/location")
public class LocationController {
    @Autowired
    private LocationService locationService;

    @GetMapping("page")
    @RequiresPermissions("wms:location:page")
    public Result<PageData<LocationDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<LocationDTO> page = locationService.page(params);

        return new Result<PageData<LocationDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:location:info")
    public Result<LocationDTO> get(@PathVariable("id") Long id) {
        LocationDTO data = locationService.get(id);

        return new Result<LocationDTO>().ok(data);
    }

    @GetMapping("home")
    public Result<HomeShowDTO> get(Map<String, Object> params) {
        HomeShowDTO data = locationService.getHomeShowDetails(params);
        return new Result<HomeShowDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:location:save")
    public Result save(@RequestBody LocationDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        locationService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:location:update")
    public Result update(@RequestBody LocationDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        locationService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:location:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        locationService.delete(ids);

        return new Result();
    }
}